package ModelLayer;

import java.util.ArrayList;

/**
 * The RentContainer class is a container with the purpose of holding Rent objects 
 */

public class RentContainer
{
    private ArrayList<Rent> rentContainer;
    private AddressBook addressBook;
    private static RentContainer instance;

    private RentContainer()
    {
        rentContainer = new ArrayList<Rent>();
    }

    /**
     * getInstance creates a rentContainer object if one does not already exist
     */
    public static RentContainer getInstance()
    {
        if(instance == null)
        {
            instance = new RentContainer();
        }
        return instance;
    }

    /**
     * addRent add an object rent to the rentContainer
     */
    
    public void addRent(Rent newRent)
    {
        rentContainer.add(newRent);  
    }

    /**
     * findRent returns a rent object by searching on a copy objects serial number
     */
    
    public Rent findRent(String serialNumber)
    {
        int index = 0;
        boolean found= false;
        int bigIndex = 0;
        while(index < rentContainer.size() && !found)
        {
            Rent rent2 = rentContainer.get(index);
            if(rent2.getCopyRented().getSerialNumber().equals(serialNumber))
            {
                found = true;
                bigIndex = index;
            }
            else
            {  
                index++;
            }
        }

        if(found == true)
        {
            return rentContainer.get(bigIndex);

        }

        else
        {
            return null;
        }

    }
    
    /**
     * findRentWithId returns a rent object by searching on its id
     */
    
    public Rent findRentWithId(int rentId)
    {
        int index = 0;
        boolean found= false;
        int bigIndex = 0;
        while(index < rentContainer.size() && !found)
        {
            Rent rent2 = rentContainer.get(index);
            if(rent2.getId() == rentId)
            {
                found = true;
                bigIndex = index;
            }
            else
            {  
                index++;
            }
        }

        if(found == true)
        {
            return rentContainer.get(bigIndex);

        }

        else
        {
            return null;
        }

    }

    
    /**
     * showRentInfo prints the information about a rent object by searching on its id
     */
    
    public void showRentInfo(int rentId)

    {
        Rent rent2 = findRentWithId(rentId);
        Copy copy2 = rent2.getCopyRented();
        Person person2 = rent2.getPerson();
        System.out.println("Titel of Dvd: " + copy2.getDvd().getTitle());
        System.out.println("Person name: " + person2.getName());
        System.out.println("Person ID: " + person2.getId());
        System.out.println("Loan ID: " + rent2.getId());
        System.out.println("Date of loan: " + rent2.getDate());
        System.out.println("Period of loan: " + rent2.getPeriod() + " days");
        System.out.println("Loan status: " + rent2.getStatus());
    }
}


